<?php

namespace CMS;

class Persist {
	private $mode = null;

	public function __construct($mode) {
		$this->mode($mode);
	}

	public function mode($mode = 'session') {
        $this->mode = $mode == 'session' ? 'session' : 'cookie';
	}

	public function __set($var, $val) {
		if($this->mode == 'session')
			$_SESSION[$var] = $val;

		if($this->mode == 'cookie') {
			$_COOKIE[$var] = $val;
			setcookie($var, (string)$val, time() + 2592000, '/');
		}
	}

	public function __get($var) {
		return $this->mode == 'session' ? $_SESSION[$var] : $_COOKIE[$var];
	}

	public function __isset($var) {
		return $this->mode == 'session' ? isset($_SESSION[$var]) : isset($_COOKIE[$var]);
	}

	public function __unset($var) {
		if($this->mode == 'session')
			unset($_SESSION[$var]);

		if($this->mode == 'cookie') {
			unset($_COOKIE[$var]);
			setcookie($var, '', time() - 2600000, '/');
		}
	}
}